<template>
  <div>
    <Content>
      <van-form>
        <van-field
          v-model="username"
          name="手机号"
          label="手机号"
          placeholder="手机号"
          :rules="[{ required: true, message: '请填写手机号' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px">
          <van-button
            round
            block
            type="info"
            native-type="submit"
            @click="onSearch"
          >
            提交
          </van-button>
        </div>
      </van-form>
    </Content>
  </div>
</template>

<script>
import Content from "../components/content/contents"; //引入子组件（容器组件）
// import { setToken } from "./../utils/storage/index.js"; //引入保存本地的封装方法
import Vue from "vue";
import { Form, Field, Button } from "vant";

Vue.use(Form);
Vue.use(Field);
Vue.use(Button);
export default {
  components: {
    //注册组件
    Content,
  },
  data() {
    return {
      username: "", //手机号
      password: "", //密码
    };
  },
  methods: {
    onSearch() {
      //点击登录
      this.$Api
        .login(this.username, this.password)
        .then((res) => {
          console.log(res.data.data.token);
          var token = res.data.data.token; //登录成功是会返回一个token
          localStorage.setItem("token", token);
          localStorage.setItem("username", this.username);
          this.$router.pushRouteByQuery("/");
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>
<style  scoped>
</style>